<?php

namespace App\Package\Attendance;

/**
 * 排班数据生成器
 *
 * Date: 22/04/2018
 * @author George
 * @package App\Package\Attendance
 */
class ScheduleGenerator
{
    /**
     * 创建排班计划
     *
     * Date: 21/04/2018
     * @author George
	 * @deprecated
     * @param Days $days
     * @param array $attributes
     * @return array
     */
    public function make(Days $days, array $attributes)
    {
        $schedules = [];
        foreach ($days->collection as $day) {
            $schedules[] = [
                'type' => $attributes['type'],
                'user_id' => $attributes['user_id'],
                'project_id' => $attributes['project_id'],
                'company_id' => $attributes['company_id'],
                'start' => $day . ' ' . $attributes['time_start'] . ':00',
                'end' => $this->endTime($attributes['time_start'], $attributes['time_end'], $day),
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ];
        }
        return $schedules;
    }

    /**
     * 根据签到时间和签退时间
     * 计算具体到某天的结束时间
     *
     * Date: 21/04/2018
     * @author George
     * @param $time_start
     * @param $time_end
     * @param string $date
     * @return string
     */
    public function endTime($time_start, $time_end, string $date)
    {
        if ($time_end < $time_start) {
            return date('Y-m-d', strtotime('+1 day', strtotime($date))) . ' ' . $time_end;
        }
        return $date . ' ' . $time_end;
    }
}
